//
//  Scene_Game.m
//  StarBugs
//
//  Created by ?? ? on 12/22/11.
//  Copyright 2011 antisword@playorca.com. All rights reserved.
//

#include "Scene/Scene_Game.h"
#include "Scene/Scene_ChapterSelect.h"
#include "Layer/FadeActionLayer.h"
#include "Scene/Scene_ChapterSelect.h"
#include "Layer/GameUILayer.h"
#include "Popup/Popup_PauseMenu.h"
#include "Popup/Popup_Tutorial.h"
#include "Popup/Popup_giftParcel.h"
#include "Util/UserData.h"
#include "Sprite/ScrollSprite.h"
#include "Util/ModuleController.h"
#include "Global.h"

#define stageSelectLayerHeight 425

using namespace cocos2d;
enum{
    kTag_gameLayer,
    kTag_stageSelectLayer,
    kTag_scroll,
    kTag_uiLayer,

    kTag_frame,
    kTag_fadelayer,
    
};
Scene_Game::~Scene_Game()
{
    if(gameLayer != NULL)
    {
        gameLayer->release();
        gameLayer = NULL;
    }
    CCTextureCache::sharedTextureCache()->removeUnusedTextures();

}
Scene_Game::Scene_Game()
{

    UserData::getInstencePtr()->currentScene = scenenum_game;
    CCTextureCache::sharedTextureCache()->removeUnusedTextures();
    winSize = CCDirector::sharedDirector()->getWinSize();
    setContentSize(CCSize(320 * ModuleController::getImageScale(), ModuleController::getSceneH() * ModuleController::getImageScale()));
    setPosition(ccp(winSize.width/2 - getContentSize().width/2, 0));
//        setPosition(ccp(winSize.width/2 - getContentSize().width/2, 0));
    setAnchorPoint(ccp(0.5, 0));
    setScale(ModuleController::getScaleFactor_w());

    setTouchEnabled(true);
#ifdef __ANDROID_
    setKeypadEnabled(true);
#endif
    setAccelerometerEnabled(true);

    changeState(gameState_init);

    addChild(GameUILayer::getInstancePtr(), kTag_uiLayer);
    GameUILayer::getInstancePtr()->setScale(1 / ModuleController::getScaleFactor_w());
    
	CCLog("Scene_Game::Scene_Game()");
    addChild(Popup_PauseMenu::getInstancePtr(), kTag_uiLayer);
    Popup_PauseMenu::getInstancePtr()->setVisible(false);

	addChild(Popup_giftParcel::getInstancePrt(), kTag_uiLayer);
	Popup_giftParcel::getInstancePrt()->setVisible(false);

    addChild(ScrollSprite::getInstancePtr(), kTag_scroll);
    ScrollSprite::getInstancePtr()->setScale(1 / ModuleController::getScaleFactor_w());

    ModuleController::checkAchievmentMetadata();
    schedule(schedule_selector(Scene_Game::tick));

}

void Scene_Game::changeState(int state)
{
	CCLog("int state is %d",state);

	gameState = state;
    switch(state)
    {
        case gameState_init:
        {

            CCLayer *_layer = CCLayer::create();

            addChild(_layer, kTag_fadelayer);

            if(ModuleController::getIsIpad())
                _layer->setPosition(ccp(-7, -57));

            gameLayer = new GameLayer();

            gameLayer->setPosition(ccp(0, 0));

            addChild((CCLayer *)gameLayer, kTag_gameLayer);
            _layer->addChild(FadeActionLayer::getInstancePtr(), kTag_fadelayer);
            _layer->setScale(ModuleController::getScaleFactor_h()/ModuleController::getScaleFactor_w());
            tick(0);
            //CCLog("gameState_init d");
            gameLayer->startGameLayer();
            //CCLog("gameState_init e");
            changeState(gameState_game);
           // CCLog("gameState_init f");
            FadeActionLayer::getInstancePtr()->startFadeIn();
            //CCLog("gameState_init g");
        }
            break;
        case gameState_gotoStageSelect:
        {
            FadeActionLayer::getInstancePtr()->startFadeOut(true);
        }
            break;
        case gameState_game:
            break;
    }
}

void Scene_Game::tick(float dt)
{
    //CCLog("Scene_Game::tick(float dt)");

//    FadeActionLayer::getInstancePtr()->setPosition(ccp(-this->getPosition().x,-getPosition().y ));
    if(gameState == gameState_init)
    {
        if(FadeActionLayer::getInstancePtr()->getActionEndFlg())
        {
            
        }
    }
    else if(gameState == gameState_gotoStageSelect)
    {
        if(FadeActionLayer::getInstancePtr()->getActionEndFlg())
        {
            FadeActionLayer::getInstancePtr()->removeFromParentAndCleanup(true);
//            removeChild(FadeActionLayer::getInstancePtr(), false);
            UserData::getInstencePtr()->scene = scenenum_game;
            CCDirector::sharedDirector()->replaceScene(Scene_ChapterSelect::scene());
        }
            
    }
}
void Scene_Game::ccTouchesBegan(CCSet *touches, CCEvent *event)
{
    CCTouch *touch = (CCTouch*)touches->anyObject();
    CCPoint location = touch->getLocationInView();
    
//    location = convertToNodeSpace(location);
    

    location = CCDirector::sharedDirector()->convertToGL(location);
//    CGPoint location = [self convertTouchToNodeSpace: touch];
//    if(ModuleController::getIsIpad())
//    {
//        location.x = 320 + (location.x - winSize.width/2) / ModuleController::getScaleFactor();
//        location.y = 480 + (location.y - winSize.height/2) / ModuleController::getScaleFactor();
//    }
    
    if(gameState == gameState_game)
    {
        gameLayer->touchBegan(location);
    }
}
void Scene_Game::ccTouchesMoved(CCSet *touches, CCEvent *event)
{
    CCTouch *touch = (CCTouch*)touches->anyObject();
    CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);
    
    if(gameState == gameState_game)
    {
        gameLayer->touchMoved(location);
    }

}
void Scene_Game::ccTouchesEnded(CCSet *touches, CCEvent *event)
{
    CCTouch *touch = (CCTouch*)touches->anyObject();
    CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);
    
    if(gameState == gameState_game)
    {
      // gameLayer->touchEnded(location);
        int flg = gameLayer->touchEnded(location);
        if(flg == gameLayerTouch_exit)
        {
            changeState(gameState_gotoStageSelect);
        }
    }
}

void Scene_Game::didAccelerate(CCAcceleration *pAccelerationValue)
{
//    CCLog("x : %f,  y : %f, z : %f",pAccelerationValue->x, pAccelerationValue->y, pAccelerationValue->z);
    gameLayer->didAccelerate(pAccelerationValue->x);
    
}

CCScene* Scene_Game::scene()
{
//    // 'scene' is an autorelease object
//    CCScene *scene = CCScene::create();
//    
//    // add layer as a child to scene
//    CCLayer* layer = new Scene_Game();
//    scene->addChild(layer);
//    layer->release();
//    
//    return scene;
    CCLayer* layer = new Scene_Game();
    layer->autorelease();
    return (CCScene *)layer;
}

void Scene_Game::callback_InApp(int type, int extraData)
{
    gameLayer->callback_inApp(type, extraData);
}

void Scene_Game::keyBackClicked()
{
#ifdef __ANDROID_
	gameLayer->beforChangeState();
    changeState(gameState_gotoStageSelect);
#endif
}